package com.ascent.client.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.IOException;

import javax.swing.*;

import com.ascent.bean.User;
import com.ascent.client.util.UserDataClient;

/**
 * 更新用户信息窗体
 */
@SuppressWarnings("serial")
public class UpdateUserFrame extends JFrame {
    private JTextField userText;
    private JTextField password;
    private JComboBox<String> authComBox;
    private JLabel tip;
    /**
     * 默认构造方法，初始化修改用户窗体
     */
    private UserDataClient userDataClient;
    public UpdateUserFrame(){
        this(new User());
    }

    /**
     * 带参数构造方法，初始化修改用户窗体
     */
    public UpdateUserFrame(User user) {
        this.setTitle("修改用户消息");

        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());

        JPanel addUserPanel = new JPanel();
        JPanel buttonPanel = new JPanel();

        JLabel userLabel = new JLabel("用户帐号：");
        JLabel passwordLabel = new JLabel("用户密码：");
        JLabel authLabel = new JLabel("用户权限：");

        userText = new JTextField(15);
        password = new JTextField(15);

        String[] options = {"普通用户", "管理员"};
        authComBox = new JComboBox<>(options);

        userText.setText(user.getUsername());
        password.setText(user.getPassword());
        String defaultAuth = options[user.getAuthority()];
        authComBox.setSelectedItem(defaultAuth);

        JButton submitButton = new JButton("提交");
        JButton exitButton = new JButton("退出");

        addUserPanel.add(userLabel);
        addUserPanel.add(new JScrollPane(userText));
        addUserPanel.add(passwordLabel);
        addUserPanel.add(new JScrollPane(password));
        addUserPanel.add(authLabel);
        addUserPanel.add(new JScrollPane(authComBox));

        buttonPanel.add(submitButton);
        buttonPanel.add(exitButton);

        setResizable(false);
        setSize(260, 180);
        setLocation(300, 100);

        JPanel tipPanel = new JPanel();

        tip = new JLabel();

        tipPanel.add(tip);

        container.add(BorderLayout.CENTER, addUserPanel);
        container.add(BorderLayout.NORTH, tip);
        container.add(BorderLayout.SOUTH,buttonPanel);

        exitButton.addActionListener(new ExitActionListener());
        submitButton.addActionListener(new AddUserActionAddListener());

        this.addWindowListener(new WindowCloser());
        this.addWindowFocusListener(new WindowFocusListener() {// 设置父窗口
            public void windowGainedFocus(WindowEvent e) {
            }
            public void windowLostFocus(WindowEvent e) {
                e.getWindow().toFront();
            }
        });
        userText.setEnabled(false);
        try {
            userDataClient = new UserDataClient();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 退出按钮事件监听
     * @author ascent
     */
    class ExitActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            setVisible(false);
            dispose();
        }
    }

    /**
     * 注册按钮事件监听
     * @author ascent
     */
    class AddUserActionAddListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            // 用户注册操作
            String username = userText.getText();
            String pass = password.getText();
            int auth = authComBox.getSelectedIndex();

            User newUser = new User(username,pass,auth);
            boolean bo = userDataClient.updateUser(newUser);
            if (bo) {
                tip.setText("修改用户成功！");
            } else {
                tip.setText("修改用户失败！");
            }
        }
    }

    /**
     * "关闭窗口"事件处理内部类
     * @author ascent
     */
    class WindowCloser extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            setVisible(false);
            dispose();
        }
    }
}
